<?php

namespace app\shop\controller\collect;

use app\common\model\collect\GoodsClaimLogModel;
use app\common\model\collect\GoodsModel;
use app\common\model\shop\PlatformModel;
use app\common\model\shop\ShopsModel;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\admin\model\SystemUser;

/**
 * 认领记录
 *
 * @Description
 * @package app\shop\controller\collect
 * @author wangqinghui
 * @date 2024-11-02 10:59
 */
class Claim extends Controller
{

    /**
     * 列表
     *
     * @Description
     * @auth true
     * @menu true
     * @author wangqinghui
     * @date 2024-11-02 11:02
     * @return void
     */
    public function index()
    {
        GoodsClaimLogModel::mQuery()->layTable(function () {
            $this->title = lang('认领记录');
            // 用户列表
            $user_list = SystemUser::getUserList($this->account_id, SystemUser::getNoTeacherUserType());
            $this->user_list = array_column($user_list, 'nickname', 'id');
        }, function (QueryHelper $query) {
            $this->check_account = false;
            $this->check_user = false;
            $query->alias('cl')->leftJoin('collect_goods cd', 'cl.collect_goods_id = cd.id');
            if(!$this->super){
                $query->where(['cl.account_id' => $this->account_id]);
                if (!$this->account_super) {
                    $query->where('cl.user_id', $this->user_id);
                }
            }
            $query->like('cd.title#title')->equal('cd.goods_code#goods_code,cl.user_id#user_id,cl.account_id#account_id,cl.user_id#user_id')->dateBetween('cl.create_at#create_at');
            $query->order('cl.id desc');
        });
    }

    /**
     * 处理列表数据
     *
     * @Description
     * @author wangqinghui
     * @date 2024-11-02 11:39
     * @param [type] $data
     * @return void
     */
    protected function _index_page_filter(&$data)
    {
        if(!empty($data)){
            // 获取平台信息
            $platform_ids = array_column($data, 'platform_id');
            $platform_data = [];
            if(!empty($platform_ids)){
                $platform_data = PlatformModel::mk()->where('id', 'in', $platform_ids)->column('title', 'id');
            }
            // 获取店铺信息
            $shop_ids = array_column($data,'shop_id');
            $shop_data = [];
            if(!empty($shop_ids)){
                $shop_data = ShopsModel::mk()->where('id', 'in', $shop_ids)->column('name', 'id');
            }

            // 获取商品图片
            $collect_goods_ids = array_column($data, 'collect_goods_id');
            $collect_goods_data = [];
            if(!empty($collect_goods_ids)){
                $collect_goods_data = GoodsModel::mk()->where('id', 'in', $collect_goods_ids)->column('main_pic', 'id');
            }

            // 获取采集人
            $user_ids = array_column($data, 'user_id');
            $claim_user_data = [];
            if(!empty($user_ids)){
                $claim_user_data = SystemUser::mk()->where('id', 'in', $user_ids)->column('nickname', 'id');
            }

            foreach($data as $key => $value){
                // 平台
                $data[$key]['platform_text'] =  !empty($platform_data[$value['platform_id']]) ? $platform_data[$value['platform_id']] : '';
                // 店铺
                $data[$key]['shop_text'] = !empty($shop_data[$value['shop_id']]) ? $shop_data[$value['shop_id']] : '';
                // 商品图片
                $data[$key]['image']  = !empty($collect_goods_data[$value['collect_goods_id']])? $collect_goods_data[$value['collect_goods_id']] : '';
                // 采集人
                $data[$key]['claim_user_text'] =!empty($claim_user_data[$value['user_id']]) ? $claim_user_data[$value['user_id']] : '';
            }
        }
    }
}